﻿
using Dapper;
using HBase;
using hconnect;
using System;
using System.Collections.Generic;
using System.Linq;
namespace hCommand {
    public abstract class AbsExtendsServer : AbsWMSBaseServer, IExtendsServer {

        public bool saveOne2Many<O, M>(One2Many<O, M> dto) where O : BaseEntity where M : ChildrenEntity {
            O doo = dto.main;
            if (doo.id == 0) {
                
                insertNotNull(doo);
            } else {
                update(doo);
            }
            if (dto.deleteIds != null&& dto.deleteIds.Count>0) {
                Type t = typeof(M);
                var p = t.GetProperty("status");
                String tableName = ToStr.GetTableName(t);
                String sql = null;
                if (p == null) {
                    sql = "delete  from " + tableName + " where id in @deleteIds and pid=@pid";
                } else {
                    sql = "update   " + tableName + " set status=-2 where id in @deleteIds and pid=@pid";
                }
                getConnection().Execute(sql,new  { dto.deleteIds,pid=dto.main.id });

            }
            if (dto.detList != null) {
                foreach (var det in dto.detList) {
                    if (det.id == 0) {
                     
                        det.pid = doo.id;
                        insertNotNull(det);
                    } else {
                        update(det);
                    }
                }
            }
            
           
            return true;
        }

    }
}
